package com.bestcem.xm.award.controller;

import com.bestcem.xm.award.controller.param.point.BandPointQueryParam;
import com.bestcem.xm.award.controller.param.point.BandPointSaveAllParam;
import com.bestcem.xm.award.controller.vo.point.BandPointDeleteCheckVO;
import com.bestcem.xm.award.controller.vo.point.BandPointListVO;
import com.bestcem.xm.award.service.BandPointService;
import com.bestcem.xm.award.service.bo.point.BandPointListBO;
import com.bestcem.xm.common.core.constant.RegTypes;
import com.bestcem.xm.common.core.domain.web.ServiceResult;
import com.bestcem.xm.common.core.domain.web.XmResultJson;
import com.bestcem.xm.common.core.uitls.StringUtil;
import com.bestcem.xm.component.security.context.SecurityContextHolder;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import static com.bestcem.xm.award.constant.PointAwardConstant.*;

/**
 * 品牌积分接口 新增、修改、删除、列表查询
 *
 * @author xa.zhang <xa.zhang@idiaoyan.com>
 * @version v1.0
 * @date 2021/8/17 14:17
 */
@RestController
@RequestMapping(value = "/award/v3/bandpoint", produces = MediaType.APPLICATION_JSON_VALUE)
public class BandPointController {

    @Resource
    private BandPointService bandPointService;

    /**
     * 批量保存删除
     *
     * @param param 参数
     * @return 结果
     */
    @PostMapping("/save")
    public XmResultJson<String> save(@RequestBody BandPointSaveAllParam param) {
        XmResultJson preCheckResult = checkSaveParam(param);
        if (preCheckResult.getCode().intValue() != SUCCESS_CODE) {
            return preCheckResult;
        }


        ServiceResult<String> result = bandPointService.save(param);
        if (result.isSuccess()) {
            return XmResultJson.success(result.getData());
        } else {
            return XmResultJson.fail(FAIL_CODE, result.getMsg());
        }
    }

    /**
     * 品牌积分删除前校验
     *
     * @param id 参数
     * @return 结果
     */
    @GetMapping("/delete/check/{id}")
    public XmResultJson<BandPointDeleteCheckVO> checkDelete(@PathVariable(value = "id") String id) {
        if (!StringUtil.checkObjectId(id)) {
            return XmResultJson.fail(FAIL_CODE, "参数错误");
        }
        ServiceResult<BandPointDeleteCheckVO> result = bandPointService.checkDelete(id);
        return XmResultJson.success(result.getData());
    }

    /**
     * 分页查询
     *
     * @return 结果
     */
    @GetMapping
    XmResultJson<BandPointListVO> findAllByPage(BandPointQueryParam param) {
        param.setOrgId(SecurityContextHolder.getOrgId());

        Integer page = param.getPage();
        Integer size = param.getSize();
        String orgId = SecurityContextHolder.getOrgId();
        if (page == null) {
            page = DEFAULT_PAGE;
        }
        if (size == null) {
            size = DEFAULT_PAGE_SIZE;
        }
        if (StringUtils.isBlank(orgId)) {
            return XmResultJson.fail(FAIL_CODE, "缺少参数orgId");
        }
        ServiceResult<BandPointListBO> result = bandPointService.findAllByPage(page, size, orgId);
        if (result.isSuccess()) {
            BandPointListBO bandPointListBO = result.getData();
            BandPointListVO bandPointListVO = new BandPointListVO();
            if (bandPointListBO == null) {
                return XmResultJson.success(bandPointListVO);
            }
            //返回结果转换为vo
            BeanUtils.copyProperties(bandPointListBO, bandPointListVO);
            List<BandPointListBO.BandPoint> boRows = bandPointListBO.getRows();
            List<BandPointListVO.BandPoint> voRows = new ArrayList<>();
            if (boRows != null && boRows.size() > 0) {
                for (BandPointListBO.BandPoint bo : boRows) {
                    if (bo != null) {
                        BandPointListVO.BandPoint vo = new BandPointListVO.BandPoint();
                        BeanUtils.copyProperties(bo, vo);
                        voRows.add(vo);
                    }
                }
            }
            bandPointListVO.setRows(voRows);
            return XmResultJson.success(bandPointListVO);
        } else {
            return XmResultJson.fail(FAIL_CODE, result.getMsg());
        }
    }

    private XmResultJson checkSaveParam(BandPointSaveAllParam param) {
        //前端不用传，后端通过token获取orgId
        param.setOrgId(SecurityContextHolder.getOrgId());
        if (StringUtils.isBlank(param.getOrgId())) {
            return XmResultJson.fail(FAIL_CODE, "缺少参数orgId");
        }

        List<BandPointSaveAllParam.DeleteParam> deleteList = param.getDeleteList();
        List<BandPointSaveAllParam.AddParam> addList = param.getAddList();

        if (deleteList == null || addList == null) {
            return XmResultJson.fail(FAIL_CODE, "缺少必填参数");
        }

        for (BandPointSaveAllParam.DeleteParam deleteParam : deleteList) {
            if (StringUtils.isBlank(deleteParam.getId())) {
                return XmResultJson.fail(FAIL_CODE, "缺少delete id");
            }
        }

        for (BandPointSaveAllParam.AddParam addParam : param.getAddList()) {
            if (StringUtils.isNotBlank(addParam.getCode()) && !Pattern.matches(RegTypes.ALL_NUMBER, addParam.getCode())) {
                return XmResultJson.fail(FAIL_CODE, addParam.getCode() + " 序号格式不对");
            }
            if (StringUtils.isBlank(addParam.getName())) {
                return XmResultJson.fail(FAIL_CODE, "名称不能为空");
            }
            boolean checkCodeFlag = addParam.getCode() != null && addParam.getCode().length() > CODE_MAX_LENGTH;
            if (addParam.getName().length() > NAME_MAX_LENGTH || checkCodeFlag) {
                return XmResultJson.fail(FAIL_CODE, "序号长度不能超过30，名称长度不能超过10");
            }
        }

        return XmResultJson.success();
    }


}